<?php

namespace App\Http\Resources;

use App\Models\Factory;
use App\Models\FactoryLike;
use Illuminate\Http\Resources\Json\ResourceCollection;

class FactoryLikeCollection extends ResourceCollection
{
    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        return [
            'code'=>200,
            'message'=>'success',
            'data'=>$this->collection->map(function ($item){
                /**
                 * @var FactoryLike $item
                 */

                if ($item->model['type'] == 1) {
                    $unit = '元/㎡';
                    $totalUnit='元';
                    $total_price=$item->model['total_price'] ?? '';
                } elseif ($item->model['type'] == 2) {
                    $unit = '元/㎡';
                    $totalUnit='元';
                    $total_price=$item->model['total_price'] ?? '';
                } else {
                    $unit = '元/月';
                    $totalUnit='元';
                    $total_price=$item->model['total_price'] ?? '';
                }
                return [
                    'factory_id' => $item->factory_id,
                    'title' => $item->model['title'],
                    'logo' => \Storage::url($item->model['logo']),
                    'tag' => collect($item->model['tag'])->pluck('name')->toArray(),
                    'address'=>$item->model['address'],
                    'type'=>$item->model['type'],
                    'total_price'=>$total_price,
                    'total_unit'=>$totalUnit,
                    'area' =>  $item->model['area'] ,
                    'price' =>$item->model['price'],
                    'price_unit'=>$unit,
                    'lat'=>$item->model['baidu_lat'],
                    'lng'=>$item->model['baidu_lng'],
                ];
            })
        ];
    }
}
